<?php
namespace ZHMVC\D;

class ModuleTable extends \ZHMVC\D\DataBase
{
    public function getRows()
    {
        return $this->_rows;
    }
    
    public function add($moduleid,$classtablename)
    {
        $IdWorker = \wantp\Snowflake\IdWorker::getIns();
        $id = $IdWorker->id();
        
        $sql="insert into zhmvc_moduletable (id,moduleid,classname) values ('".$id."',:moduleid,:classname)";
        $bind=array(":moduleid"=>$moduleid,":classname"=>$classtablename);
        $this->_db->update($sql,$bind);
        $this->_lastid=$id;
        $this->addColumns($moduleid,$classtablename);
        return 1;
    }
    
    private function addColumns($moduleid,$classtablename)
    {
        //根据模块id得到前缀
        $sql="select biaoqianzhui from zhmvc_module where id='".$moduleid."'";
        $data=$this->_db->getOne($sql);
        $biaoqianzhui=$data['biaoqianzhui'];
        
        $tablename=strtolower($biaoqianzhui.$classtablename);
        $rs = new \ZHMVC\D\ShowTables();
        $s = $rs->getColumns($tablename);
        
        for ($i = 0, $imax = count($s); $i < $imax; $i ++) {
            $liename="";
            if($s[$i]['comment']!="")
            {
                $liename=$s[$i]['comment'];
            }
            else
            {
                $liename=$s[$i]['field'];
            }
            $sql2="select id,isshow,xuhao from zhmvc_yctable where `datatable`='".$tablename."' and liename='".$s[$i]['field']."'";
            $data2 = $rs->getSqlOne($sql2);
            $rows2 = $rs->getRowCount();
            //echo $rows;
            if($rows2>0)
            {
                
            }
            else
            {
                $IdWorker = \wantp\Snowflake\IdWorker::getIns();
                $id = $IdWorker->id();
                $sql1="insert into zhmvc_yctable(`id`,`datatable`,`liename`,`colindex`,`lietagname`,`isshow`,`xuhao`) values ('".$id."','".$tablename."','".$s[$i]['field']."','0','".$s[$i]['comment']."','1','0')";
                $rs->SqlUpdate($sql1);
            }
        }
        
    }
    
    public function getAllModule()
    {
        $sql="select * from zhmvc_moduletable";
        $data=$this->_db->getAll($sql);
        $this->_rows=$this->_db->getRowCount();
        return $data;
    }
    
    public function getAllModuleForMid($moduleid)
    {
        $sql="select * from zhmvc_moduletable where moduleid=:moduleid";
        $bind=array(":moduleid"=>$moduleid);
        $data=$this->_db->getAll($sql,$bind);
        $this->_rows=$this->_db->getRowCount();
        return $data;
    }
    
    public function getAllNum($parameter="")
    {
        if(is_array($parameter)==true)
        {
            $bind=array();
            $s="";
            $tempS=parent::parseSql($parameter);
            $bind=$tempS["bind"];
            $s=$tempS["s"];
            
            if($s=="")
            {
                $sql="select count(*) as num from zhmvc_moduletable";
                $data=$this->_db->getOne($sql);
            }
            else
            {
                //去掉第一个逻辑符号
                $s_a=explode(" ", $s);
                $tempS="";
                for($j=1;$j<count($s_a);$j++)
                {
                    $tempS.=$s_a[$j]." ";
                }
                
                $sql="select count(*) as num from zhmvc_moduletable where ".$tempS;
                $data=$this->_db->getOne($sql,$bind);
            }
        }
        elseif($parameter!="")
        {
            $sql="select count(*) as num from zhmvc_moduletable where ".$parameter;
            $data=$this->_db->getOne($sql);
        }
        else
        {
            $sql="select count(*) as num from zhmvc_moduletable";
            $data=$this->_db->getOne($sql);
        }
        if(empty($data)==true)
        {
            $data["num"]=0;
        }
        $this->_rows=$this->_db->getRowCount();
        return $data;
    }
    
    public function getAll($parameter="")
    {
        if(is_array($parameter)==true)
        {
            $bind=array();
            $s="";
            $tempS=parent::parseSql($parameter);
            $bind=$tempS["bind"];
            $s=$tempS["s"];
            
            if($s=="")
            {
                $sql="select * from zhmvc_moduletable";
                $datas=$this->_db->getAll($sql);
            }
            else
            {
                $s_a=explode(" ", $s);
                $tempS="";
                for($j=1;$j<count($s_a);$j++)
                {
                    $tempS.=$s_a[$j]." ";
                }
                $sql="select * from zhmvc_moduletable where ".$tempS;
                $datas=$this->_db->getAll($sql,$bind);
            }
        }
        elseif($parameter!="")
        {
            $sql="select * from zhmvc_moduletable where ".$parameter."";
            $datas=$this->_db->getAll($sql);
        }
        else
        {
            $sql="select * from zhmvc_moduletable";
            $datas=$this->_db->getAll($sql);
        }
        
        $this->_rows=$this->_db->getRowCount();
        return $datas;
    }
    
    public function getPages($limit,$parameter="")
    {
        if(is_array($parameter)==true)
        {
            $bind=array();
            $s="";
            $tempS=parent::parseSql($parameter);
            $bind=$tempS["bind"];
            $s=$tempS["s"];
            if($s=="")
            {
                $sql="select * from zhmvc_moduletable order by id desc LIMIT ".$limit."";
                $datas=$this->_db->getAll($sql);
            }
            else
            {
                $s_a=explode(" ", $s);
                $tempS="";
                for($j=1;$j<count($s_a);$j++)
                {
                    $tempS.=$s_a[$j]." ";
                }
                $sql="select * from zhmvc_moduletable where ".$tempS." order by id desc LIMIT ".$limit."";
                $datas=$this->_db->getAll($sql,$bind);
            }
        }
        elseif($parameter!="")
        {
            $sql="select * from zhmvc_moduletable where ".$parameter." order by id desc LIMIT ".$limit."";
            $datas=$this->_db->getAll($sql);
        }
        else
        {
            $sql="select * from zhmvc_moduletable order by id desc LIMIT ".$limit."";
            $datas=$this->_db->getAll($sql);
        }
        $this->_rows=$this->_db->getRowCount();
        return $datas;
    }
    
    public function getOne($postid)
    {
        $sql="select * from zhmvc_moduletable where id=:id";
        $bind=array(":id"=>$postid);
        $data=$this->_db->getOne($sql,$bind);
        $this->_rows=$this->_db->getRowCount();
        return $data;
    }
    
    public function getOne1($parameter="")
    {
        if(is_array($parameter)==true)
        {
            $bind=array();
            $s="";
            $tempS=parent::parseSql($parameter);
            $bind=$tempS["bind"];
            $s=$tempS["s"];
            
            if($s=="")
            {
                $sql="select * from zhmvc_moduletable";
                $datas=$this->_db->getOne($sql);
            }
            else
            {
                $s_a=explode(" ", $s);
                $tempS="";
                for($j=1;$j<count($s_a);$j++)
                {
                    $tempS.=$s_a[$j]." ";
                }
                $sql="select * from zhmvc_moduletable where ".$tempS;
                $datas=$this->_db->getOne($sql,$bind);
            }
        }
        elseif($parameter!="")
        {
            $sql="select * from zhmvc_moduletable where ".$parameter."";
            $datas=$this->_db->getOne($sql);
        }
        else
        {
            $sql="select * from zhmvc_moduletable";
            $datas=$this->_db->getOne($sql);
        }
        
        $this->_rows=$this->_db->getRowCount();
        return $datas;
    }
    
    public function add1($moduleid,$classname,$info,$mtype='list')
    {
        $IdWorker = \wantp\Snowflake\IdWorker::getIns();
        $id = $IdWorker->id();
        $sql="insert into zhmvc_moduletable (`id`,`moduleid`,`classname`,`info`,`mtype`) values (:id,:moduleid,:classname,:info,:mtype)";
        $bind=array(":id"=>$id,":moduleid"=>$moduleid,":classname"=>$classname,":info"=>$info,":mtype"=>$mtype);
        $this->_db->update($sql,$bind);
        $this->_lastid=$id;
        return $this->_lastid;
    }
    
    public function update($id,$moduleid,$classname,$info,$mtype='list')
    {
        $sql="update zhmvc_moduletable set `moduleid`=:moduleid,`classname`=:classname,`info`=:info,`mtype`=:mtype where id=:id";
        $bind=array(":moduleid"=>$moduleid,":classname"=>$classname,":info"=>$info,":mtype"=>$mtype,":id"=>$id);
        $this->_db->update($sql,$bind);
        return 1;
    }
    
    public function delete($id)
    {
        $sql="delete from zhmvc_moduletable where id=:id";
        $bind=array(":id"=>$id);
        $this->_db->update($sql,$bind);
        return 1;
    }
    
    public function addOne()
    {
        $IdWorker = \wantp\Snowflake\IdWorker::getIns();
        $id = $IdWorker->id();
        $sql = "insert into zhmvc_moduletable(`id`) values (:id)";
        //echo $sql;
        $bind = array(":id" => $id);
        $this->_db->update($sql, $bind);
        $this->_lastid = $id;
        return 1;
    }
    
    public function getLastId(){
        return $this->_lastid;
    }
    
}